package org.example.petsystem.controller;

import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.service.impl.OrderServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/OrderMessage")
@CrossOrigin(origins="*")
public class OrderController {
    @Autowired
    private OrderServiceimpl orderService;

    //增加商品订单（卖家）
    @RequestMapping("/addOrder")
    public ResultBody addOrder(@RequestBody Map<String, Object> map) {
        //判断参数是否正确
        if (map.get("OrderCode") == null || map.get("userphone") == null || 
            map.get("GoodsName") == null || map.get("price") == null || 
            map.get("buyphone") == null || map.get("GoodsCode") == null ||
            map.get("quantity") == null || map.get("address") == null ||
            map.get("userName") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        
        // 验证buyphone是否存在于user表中
        String buyphone = (String) map.get("buyphone");
        if (!orderService.checkUserExists(buyphone)) {
            return new ResultBody(400, "卖家手机号不存在", false);
        }
        
        // 安全地转换quantity
        try {
            if (map.get("quantity") instanceof Integer) {
                // 已经是Integer类型，不需要转换
            } else if (map.get("quantity") instanceof String) {
                map.put("quantity", Integer.parseInt((String) map.get("quantity")));
            } else {
                return new ResultBody(400, "购买数量格式错误", false);
            }
        } catch (NumberFormatException e) {
            return new ResultBody(400, "购买数量格式错误", false);
        }
        
        // 安全地转换price
        try {
            Object priceObj = map.get("price");
            if (priceObj instanceof Integer) {
                map.put("price", ((Integer) priceObj).doubleValue());
            } else if (priceObj instanceof Double) {
                // 已经是Double类型，不需要转换
            } else if (priceObj instanceof String) {
                map.put("price", Double.parseDouble((String) priceObj));
            } else {
                return new ResultBody(400, "价格格式错误", false);
            }
        } catch (NumberFormatException e) {
            return new ResultBody(400, "价格格式错误", false);
        }
        
        return orderService.addOrder(map);
    }
//我买到的订单信息（买家）
    @RequestMapping("/displayOrderU")
    public ResultBody displayOrder(@RequestBody Map<String, Object> map) {
        if (map.get("userphone") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String user_phone = (String) map.get("userphone");
        return orderService.displayOrderU(user_phone);
    }
//我卖出的订单信息（卖家）
    @RequestMapping("/displayOrderB")
    public ResultBody displayOrderB(@RequestBody Map<String, Object> map) {
        if (map.get("buyphone") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String Buyers_phone = (String) map.get("buyphone");
        return orderService.displayOrderB(Buyers_phone);
    }
    //修改订单状态
    @RequestMapping("/updateOrderStatus")
    public ResultBody updateOrderStatus(@RequestBody Map<String, Object> map) {
        if (map.get("code") == null || map.get("status") == null||map.get("Expresscode")==null) {
            return new ResultBody(404, "缺少参数", false);
        }
        return orderService.updateOrderStatus(map);
    }
}
